package com.mnkjxy.beauty.view;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.mnkjxy.beauty.net.HttpClientUtils;
import com.mnkjxy.beauty.net.elements.LoginElement;
import com.mnkjxy.beauty.net.elements.RegisterElement;
import com.mnkjxy.beauty.net.elements.UpdateUserInfoElement;
import com.mnkjxy.beauty.net.elements.headImageUpload;
import com.mnkjxy.beauty.net.protocal.Message;
import com.mnkjxy.beauty.values.Constant;
import com.mnkjxy.beauty.values.Views;
import com.mnkjxy.beauty.view.manager.BasicView;

public class FirstView extends BasicView {

	private Button button;
	private Button login;
	private Button updatauserinfo;
	private Button upload;

	/**
	 * 构造中初始化视图,保证视图只会被创建一次,提升效率
	 * 
	 * @param context
	 */
	public FirstView(Context context) {
		super(context);
	}

	protected void init() {
		showInMiddle = new LinearLayout(context);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		showInMiddle.setLayoutParams(params);

		button = new Button(context);
		button.setText("注册");
		showInMiddle.addView(button);

		login = new Button(context);
		login.setText("登录");
		showInMiddle.addView(login);

		updatauserinfo = new Button(context);
		updatauserinfo.setText("修改资料");
		showInMiddle.addView(updatauserinfo);

		upload = new Button(context);
		upload.setText("上传文件");
		showInMiddle.addView(upload);

		showInMiddle.setBackgroundColor(Color.BLUE);

	}

	@Override
	protected int getId() {
		// TODO Auto-generated method stub
		return Views.VIEW_FIRST_VIEW;
	}

	@Override
	protected void setLinstener() {
		// TODO Auto-generated method stub
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new myHttpTask<Void, Void, Void>() {

					@Override
					protected Void doInBackground(Void... params) {

						Message message = new Message();
						RegisterElement element = new RegisterElement();
						String xml = message.getXml(element);

						HttpClientUtils.sendXml(Constant.IP_ADDRESS_XML, xml);
						return null;
					}

				}.execute();
			}
		});

		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new myHttpTask<Void, Void, Void>() {

					@Override
					protected Void doInBackground(Void... params) {

						Message message = new Message();
						LoginElement element = new LoginElement();
						String xml = message.getXml(element);

						HttpClientUtils.sendXml(Constant.IP_ADDRESS_XML, xml);
						return null;
					}

				}.execute();
			}
		});

		updatauserinfo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new myHttpTask<Void, Void, Void>() {

					@Override
					protected Void doInBackground(Void... params) {

						Message message = new Message();
						UpdateUserInfoElement element = new UpdateUserInfoElement();
						String xml = message.getXml(element);

						HttpClientUtils.sendXml(Constant.IP_ADDRESS_XML, xml);
						return null;
					}

				}.execute();
			}
		});

		upload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				HttpClientUtils.sendImage(Constant.IP_ADDRESS_IMAGE, new headImageUpload(), new RequestCallBack<Object>() {

					@Override
					public void onSuccess(ResponseInfo<Object> responseInfo) {
						// TODO Auto-generated method stub
						System.out.println(responseInfo.result.toString());
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// TODO Auto-generated method stub

					}
				});
			}
		});

	}

}
